package com.huqing.icu.dao;

import com.huqing.icu.dto.HouseDto;
import com.huqing.icu.dto.HouseQueryDto;
import com.huqing.icu.mapper.HouseApproveRecordMapper;
import com.huqing.icu.mapper.HouseMapper;
import com.huqing.icu.model.House;
import com.huqing.icu.model.HouseApproveRecord;
import com.huqing.icu.utils.CopyUtils;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Description 房源dao
 * @Author huqing
 * @Date 2025/9/13 18:58
 **/
@Repository
public class HouseDao {

    @Resource
    private HouseMapper houseMapper;

    @Resource
    private HouseApproveRecordMapper houseApproveRecordMapper;

    public Integer insert(House house) {
        return houseMapper.insert(house);
    }
    public Integer updateById(House house) {
        return houseMapper.updateById(house);
    }

    public HouseDto selectById(Long id) {
        House house = houseMapper.selectById(id);
        if (house == null) {
            return null;
        }
        HouseDto houseDto = CopyUtils.copy(house, HouseDto.class);
        // 搜索最新的一条审核记录
        HouseApproveRecord houseApproveRecord = houseApproveRecordMapper.queryLastRecordByHouseId(id);
        houseDto.setApproveStatus(houseApproveRecord.getApproveStatus());
        houseDto.setApproveContent(houseApproveRecord.getApproveContent());
        houseDto.setApproveUserId(houseApproveRecord.getApproveUserId());
        houseDto.setApproveTime(houseApproveRecord.getApproveTime());
        return houseDto;
    }

    public House selectOneByUserId(Long userId, Long id, Integer isDeleted) {
        return houseMapper.selectOneByUserId(userId, id, isDeleted);
    }

    public Integer getNumByCondition(HouseQueryDto queryDto) {
       return houseMapper.getNumByCondition(queryDto);
    }

    public List<HouseDto> selectByCondition(HouseQueryDto queryDto){
        return houseMapper.selectByCondition(queryDto);
    }

    public Integer updateStatus(Long id, Long userId, Integer status) {
        return houseMapper.updateStatus(id, userId, status);
    }

    public Integer deleteById(Long id) {
        return houseMapper.deleteById(id);
    }




    /************************************   house_approve_record   **************************************/
    public Integer deleteApproveRecordByHouseId(Long houseId) {
        return houseApproveRecordMapper.deleteByHouseId(houseId);
    }

    public Integer insertHouseApproveRecord(HouseApproveRecord houseApproveRecord) {
        return houseApproveRecordMapper.insert(houseApproveRecord);
    }

    public Integer updateApproveRecord(HouseApproveRecord houseApproveRecord) {
        return houseApproveRecordMapper.updateById(houseApproveRecord);
    }

    public HouseApproveRecord selectApproveRecordById(Long id) {
        return houseApproveRecordMapper.selectById(id);
    }

    public List<HouseApproveRecord> selectApproveRecordByHouseIdList(List<Long> houseIdList) {
        return houseApproveRecordMapper.selectByHouseIdList(houseIdList);
    }

}
